<template>
  <div class="app-container">
    <div class="v-num">
      <div class="tab">
        <div class="title">
          我的待办
        </div>
        <div class="num">
          {{ NumberData.myNeedDoNumb }}个任务
        </div>
      </div>
      <div class="tab">
        <div class="title">
          我创建的任务数
        </div>
        <div class="num">
          {{ NumberData.myCreateNumb }}个任务
        </div>
      </div>
      <div class="tab">
        <div class="title">
          本周完成任务数
        </div>
        <div class="num">
          {{ NumberData.completeNumb }}个任务
        </div>
      </div>
    </div>

    <div class="createManager">
      <router-link v-permission="['inspect:create']" to="createPatrol">
        <div style="margin: 0 0 0 -5px;" class="addbtn">创建问题单</div>
      </router-link>
      <div class="addbtn down" @click="downList">
        导出
      </div>
    </div>
    <div class="seatch">
      <el-input
        v-model="listQuery.keyword"
        placeholder="按名称搜索"
        @keyup.enter.native="searchName($event)"
      >
        <i slot="prefix" class="el-input__icon el-icon-search" @click="getList()" />
      </el-input>
    </div>
    <div class="content">
      <el-table
        v-loading="listLoading"
        align="center"
        :data="tableData"
        style="width: 100%"
        @filter-change="handleFilterChange"
      >
        <el-table-column
          prop="number"
          align="center"
          label="问题单编号"
        />
        <el-table-column
          prop="status"
          align="center"
          label="状态"
          :filter-multiple="false"
          :column-key="'status'"
          :filters="statusType"
          filter-placement="bottom-end"
        >
          <template slot-scope="{row}">
            <div>
              <div><span :class="row.status | statusTypeFilter" />
                <span>{{ row.status | nameFilter }}</span>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="type"
          label="问题单类型"
          :filter-multiple="false"
          :column-key="'type'"
          :filters="typeStatusList"
          filter-placement="bottom-end"
        >
          <template slot-scope="{row}">
            <div>{{ row.typeName }}</div>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="content"
          label="检查内容"
        />
        <el-table-column
          align="center"
          prop="creatorName"
          label="创建人"
        />
        <el-table-column
          align="center"
          prop="createAt"
          label="创建时间"
        />
        <el-table-column
          align="center"
          label="操作"
        >
          <template slot-scope="{row}">
            <div class="operate">
              <div>
                <router-link :to="{ path: 'detailPatrol', query: { id: row.id }}">详情</router-link>
              </div>
              <div v-if="row.status==='solving'||row.status==='reject'" v-permission="['inspect:check']">
                <router-link :to="{ path: 'edgePatrol', query: {id: row.id }}">报验</router-link>
              </div>
              <div v-else-if="row.status==='recovering'" v-permission="['inspect:reply']">
                <router-link :to="{ path: 'edgePatrol', query: {id: row.id }}">验收</router-link>
              </div>
              <div v-permission="['inspect:delete']" @click="deletePatrol(row)">
                删除
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="pageination-div">
        <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="getList" />
      </div>

    </div>
    <delete-dialog
      dislog-name="问题单"
      :dialog-del-visible="dialogDelVisible"
      :title="'问题单'"
      @funcancl="funcancl"
      @func="deleteData"
    />
  </div>

</template>

<script>
import { Message } from 'element-ui'
import Pagination from '@/components/pagination'
import { inspectList, deleteInspect, getInspectType, getNumber } from '@/api/patrolExamine'
import DeleteDialog from '@/components/dialog/delete'
export default {
  filters: {
    statusTypeFilter(status) {
      const statusMap = {
        unsolved: 'unsolvedColor',
        solving: 'solvingColor',
        recovering: 'recoveringColor',
        reject: 'rejectdColor',
        success: 'resolvedColor'
      }
      return statusMap[status]
    },
    nameFilter(status) {
      const statusMap = {
        unsolved: '未解决',
        solving: '解决中',
        recovering: '报验中',
        reject: '已驳回',
        success: '已验收'
      }
      return statusMap[status]
    },
    statusFilter(status) {
      const statusMap = {
        quality: '质量问题',
        security: '安全问题'
      }
      return statusMap[status]
    }
  },
  components: {
    Pagination,
    DeleteDialog
  },
  data() {
    return {
      pathUrl: process.env.VUE_APP_BASE_API,
      tableData: [],
      NumberData: {},
      total: 0,
      listLoading: true,
      dialogDelVisible: false,
      teamDetail: {},
      listQuery: {
        keyword: null,
        type: null,
        status: null,
        pageNum: 1,
        pageSize: 10
      },
      statusType: [
        { value: 'unsolved', text: '未解决' },
        { value: 'solving', text: '解决中' },
        { value: 'recovering', text: '报验中' },
        { value: 'reject', text: '已驳回' },
        { value: 'success', text: '已验收' }
      ],
      typeStatusList: [
        // { value: 'quality', text: '质量问题' },
        // { value: 'security', text: '安全问题' }
      ],
      inspectType: []
    }
  },
  mounted() {
    this.getList()
    this.getNumberData()
    this.getInspectType()
  },
  methods: {
    sizeChange(val) {
      this.pageNum = val
    },
    currentChange(val) {
      this.currentPage = val
    },
    searchName(e) {
      this.listQuery.keyword = e.target.value
      this.getList()
    },
    handleFilterChange(filters, index) {
      if (filters.status) {
        this.listQuery.status = filters.status[0]
      }
      if (filters.type) {
        this.listQuery.type = filters.type[0]
      }
      this.getList()
    },
    // 导出
    downList() {
      if (this.listQuery.type !== null && this.listQuery.status !== null) {
        const url = this.pathUrl + '/inspect/download?' + 'type=' + this.listQuery.type + '&status=' + this.listQuery.status
        window.open(url)
      } else if (this.listQuery.type === null && this.listQuery.status !== null) {
        const url = this.pathUrl + '/inspect/download?' + '&status=' + this.listQuery.status
        window.open(url)
      } else if (this.listQuery.type !== null && this.listQuery.status === null) {
        const url = this.pathUrl + '/inspect/download?' + 'type=' + this.listQuery.type
        window.open(url)
      } else {
        const url = this.pathUrl + '/inspect/download'
        window.open(url)
      }
    },
    // 问题单类型
    async getInspectType() {
      const res = await getInspectType()
      if (res.code === 200) {
        this.inspectType = res.data
        this.inspectType.map((val, index) => {
          this.typeStatusList.push({ value: val.id, text: val.name, type: val.type })
        })
      } else {
        Message({
          message: '问题单类型查询出错!',
          type: 'error',
          showClose: true,
          duration: 2 * 1000
        })
      }
    },
    // 获取数据
    async getList() {
      const res = await inspectList(this.listQuery)
      this.listLoading = false
      if (res.code === 200) {
        this.tableData = res.data.list
        this.total = res.data.total || 0
        this.pages = res.data.pages || 0
      } else {
        Message({
          message: res.data.msg || 'error',
          type: 'error',
          showClose: true,
          duration: 2 * 1000
        })
      }
    },
    async getNumberData() {
      const res = await getNumber()
      if (res.code === 200) {
        this.NumberData = res.data
      } else {
        Message({
          message: res.msg || 'error',
          type: 'error',
          showClose: true,
          duration: 2 * 1000
        })
      }
    },
    deletePatrol(data) {
      this.dialogDelVisible = true
      this.teamDetail = data
    },
    // 删除
    async deleteData(val) {
      const id = this.teamDetail.id
      const res = await deleteInspect(id)
      if (res.code === 200) {
        this.dialogDelVisible = false
        this.getList()
        this.$message({
          type: 'success',
          message: '删除成功!',
          showClose: true,
          center: true,
          duration: 2 * 1000
        })
      } else {
        this.dialogDelVisible = false
      }
    },
    funcancl(val) {
      this.dialogDelVisible = false
    }

  }
}
</script>
<style lang="scss" scoped>
@import '../../styles/variables';
.app-container{
  .v-num{
    height: 148px;
    display: flex;
    padding-bottom: 10px;
    border-bottom: 1px solid rgba(221,221,221,0.6);
    .tab{
      flex: 1;
      text-align: center;
      margin-top: 50px;
      .title{
        font-size:14px;
        font-weight:400;
        color:rgba(255,255,255,0.65);
        padding-bottom: 10px;
      }
      .num{
        font-size:18px;
        font-weight:600;
        color:rgba(255,255,255,0.85);
      }
    }
  }
  .createManager{
    position: absolute;
    margin: 13px 0 0 26px;
    font-size:$fontSize14;
    font-weight:400;
    color:rgba(255,255,255,0.65);
   .down{
      margin: 0 0 0 10px;
       background: rgba(247, 181, 0, 0.8);
   }
  }
  .content{
    .operate{
      // display: flex;
      font-size:$fontSize12;
      font-weight:400;
      color:rgba(247,181,0,1);
      div{
        cursor: pointer;
        // flex: 1;
      }
    }
  }
}
</style>
